package cn.stylefeng.guns.modular.sht.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.sht.entity.History;
import cn.stylefeng.guns.modular.sht.mapper.HistoryMapper;
import cn.stylefeng.guns.modular.sht.mapper.QuestionMapper;
import cn.stylefeng.guns.modular.sht.model.params.HistoryParam;
import cn.stylefeng.guns.modular.sht.model.result.HistoryResult;
import cn.stylefeng.guns.modular.sht.service.HistoryService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 学习历史 服务实现类
 * </p>
 *
 * @author
 * @since 2021-01-22
 */
@Service
public class HistoryServiceImpl extends ServiceImpl<HistoryMapper, History> implements HistoryService {
    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public void add(HistoryParam param) {
        History entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(HistoryParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(HistoryParam param) {
        History oldEntity = getOldEntity(param);
        History newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public HistoryResult findBySpec(HistoryParam param) {
        return null;
    }

    @Override
    public List<HistoryResult> findListBySpec(HistoryParam param) {
        return this.baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(HistoryParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        List<HistoryResult> records = page.getRecords();
        records.forEach(r -> {
            setStr(r);
        });
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo wrongList(HistoryParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.wrongPageList(pageContext, param);
        List<HistoryResult> records = page.getRecords();
        records.forEach(r -> {
            setStr(r);
        });
        return LayuiPageFactory.createPageInfo(page);
    }

    private void setStr(HistoryResult r) {
        if (StringUtils.equalsIgnoreCase("A", r.getSelectAnswer())) {
            r.setSelectAnswerStr(questionMapper.selectById(r.getQuestionId()).getAnswerA());
        } else if (StringUtils.equalsIgnoreCase("B", r.getSelectAnswer())) {
            r.setSelectAnswerStr(questionMapper.selectById(r.getQuestionId()).getAnswerB());
        } else if (StringUtils.equalsIgnoreCase("C", r.getSelectAnswer())) {
            r.setSelectAnswerStr(questionMapper.selectById(r.getQuestionId()).getAnswerC());
        } else if (StringUtils.equalsIgnoreCase("D", r.getSelectAnswer())) {
            r.setSelectAnswerStr(questionMapper.selectById(r.getQuestionId()).getAnswerD());
        }

        if (StringUtils.equalsIgnoreCase("A", r.getAnswerOk())) {
            r.setAnswerOkStr(questionMapper.selectById(r.getQuestionId()).getAnswerA());
        } else if (StringUtils.equalsIgnoreCase("B", r.getAnswerOk())) {
            r.setAnswerOkStr(questionMapper.selectById(r.getQuestionId()).getAnswerB());
        } else if (StringUtils.equalsIgnoreCase("C", r.getAnswerOk())) {
            r.setAnswerOkStr(questionMapper.selectById(r.getQuestionId()).getAnswerC());
        } else if (StringUtils.equalsIgnoreCase("D", r.getAnswerOk())) {
            r.setAnswerOkStr(questionMapper.selectById(r.getQuestionId()).getAnswerD());
        }
    }

    private Serializable getKey(HistoryParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private History getOldEntity(HistoryParam param) {
        return this.getById(getKey(param));
    }

    private History getEntity(HistoryParam param) {
        History entity = new History();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
